# coding: utf-8
################################################################################
#* Licensed Materials - Property of IBM  
#*  
#* OCO Source Materials  
#*  
#* (C) Copyright IBM Corp. 2014 All Rights Reserved  
#*  
#* The source code for this program is not published or other-  
#* wise divested of its trade secrets, irrespective of what has  
#* been deposited with the U.S. Copyright Office.  
################################################################################

################################################################################
# Author:yushaoyang@cn.ibm.com
# Createdate:Sun May 10 22:10:25 2015
# Lastupd:Sun May 10 22:10:25 2015

# Description
# Updatelog:
# 1. "create the file"
################################################################################

module Query
  module ExtModel
    module ExtDataService
      # for datacore's class methods
      def self.included datacore

        def datacore._DBModel
          Query::DBModel::Patient
        end

        def datacore._hasValue var
          if var.nil? || var.empty?
            return true
          else
            return false
          end
        end

        def datacore.get_lastTime attrs
          if attrs["pid"] != nil
            #Part1: DB access
            _patient=Query::DBModel::Patient.where(:patientid => attrs["pid"]).first
            _rawdata=Query::DBModel::RawData.where(:patient_id => _patient).last
            _analysedData=Query::DBModel::AnalysedData.where(:patient_id => _patient).last
            #Part2: data refactor
            _rtime=_rawdata.etime unless _rawdata.nil?
            _atime=_analysedData.etime unless _analysedData.nil?
            _returnData={:rawdata => _rtime,:analysedData => _atime}
            result=JSON.generate(_returnData)
          end
        end

        def datacore.get_raw attrs
          _data=JSON.unparse(attrs)
          _pid,_stime,_etime = attrs['pid'],attrs['stime'],attrs['etime']
          _patient=Query::DBModel::Patient.where(:patientid => _pid).last
          if !_hasValue(_stime) && !_hasValue(_etime)
            #have two time
            log.debug "*have both time point*"
            _rawdata=Query::DBModel::RawData.where(:patient_id => _patient, :stime.gte =>Time.parse(_stime), :etime.lte => Time.parse(_etime))
          elsif !_hasValue(_stime) && _hasValue(_etime)
            #only start time
            log.debug "*only start time*"
            _rawdata=Query::DBModel::RawData.where(:patient_id => _patient, :stime.gte => Time.parse(_stime))
          elsif _hasValue(_stime)  && !_hasValue(_etime)
            #only end time
            log.debug "*only end time*"
            _rawdata=Query::DBModel::RawData.where(:patient_id => _patient, :etime.lte => Time.parse(_etime))
          else
            _rawdata=nil
          end
          #TODO from TFS
          rawData='happyreturn'
          _data.rawdata=rawData
          result=_data
        end

        def datacore.get_analysed attrs
          _data=JSON.unparse(attrs)
          _pid,_stime,_etime = attrs['pid'],attrs['stime'],attrs['etime']
          _patient=Query::DBModel::Patient.where(:patientid => _pid).last
          if !_hasValue(_stime) && !_hasValue(_etime)
            #have two time
            log.debug "*have both time point*"
            _analysedData=Query::DBModel::AnalysedData.where(:patient_id => _patient, :stime.gte =>Time.parse(_stime), :etime.lte => Time.parse(_etime))
          elsif !_hasValue(_stime) && _hasValue(_etime)
            #only start time
            log.debug "*only start time*"
            _analysedData=Query::DBModel::AnalysedData.where(:patient_id => _patient, :stime.gte => Time.parse(_stime))
          elsif _hasValue(_stime)  && !_hasValue(_etime)
            #only end time
            log.debug "*only end time*"
            _analysedData=Query::DBModel::AnalysedData.where(:patient_id => _patient, :etime.lte => Time.parse(_etime))
          else
            _analysedData=nil
          end
          #TODO from TFS
          _analysedData='happyreturn'
          _data._analysedData=_analysedData
          result=_data
        end

        def datacore.get_event attrs
          _data=JSON.unparse(attrs)
          _pid,_stime,_etime = attrs['pid'],attrs['stime'],attrs['etime']
          _patient=Query::DBModel::Patient.where(:patientid => _pid).last
          if !_hasValue(_stime) && !_hasValue(_etime)
            #have two time
            log.debug "*have both time point*"
            _analysedEvent=Query::DBModel::AnalysedEvent.where(:patient_id => _patient, :stime.gte =>Time.parse(_stime), :etime.lte => Time.parse(_etime))
          elsif !_hasValue(_stime) && _hasValue(_etime)
            #only start time
            log.debug "*only start time*"
            _analysedEvent=Query::DBModel::AnalysedEvent.where(:patient_id => _patient, :stime.gte => Time.parse(_stime))
          elsif _hasValue(_stime)  && !_hasValue(_etime)
            #only end time
            log.debug "*only end time*"
            _analysedEvent=Query::DBModel::AnalysedEvent.where(:patient_id => _patient, :etime.lte => Time.parse(_etime))
          else
            _analysedEvent=nil
          end
          #TODO from TFS
          _analysedEvent='happyreturn'
          _data._analysedEvent=_analysedEvent
          result=_data
        end

        def datacore.get_rawIndex attrs
          _data=JSON.unparse(attrs)
          _pid,_stime,_etime = attrs['pid'],attrs['stime'],attrs['etime']
          _patient=Query::DBModel::Patient.where(:patientid => _pid).last
          if !_hasValue(_stime) && !_hasValue(_etime)
            #have two time
            log.debug "*have both time point*"
            _analysedEvent=Query::DBModel::RawData.where(:patient_id => _patient, :stime.gte =>Time.parse(_stime), :etime.lte => Time.parse(_etime))
          elsif !_hasValue(_stime) && _hasValue(_etime)
            #only start time
            log.debug "*only start time*"
            _analysedEvent=Query::DBModel::RawData.where(:patient_id => _patient, :stime.gte => Time.parse(_stime))
          elsif _hasValue(_stime)  && !_hasValue(_etime)
            #only end time
            log.debug "*only end time*"
            _analysedEvent=Query::DBModel::RawData.where(:patient_id => _patient, :etime.lte => Time.parse(_etime))
          else
            _analysedEvent=nil
          end
          #TODO from TFS
          _analysedEvent='happyreturn'
          _data._analysedEvent=_analysedEvent
          result=_data
        end

        def datacore.fill_testData
          patient = Query::DBModel::Patient.new
          patient.patientid = "notcool"
          patient.rawdata_attributes={
              "0" => {stime: Time.now-60*60*2, etime: Time.now, url: "www.xuz1.com"},
              "1" => {stime: Time.now-60*60*3, etime: Time.now, url: "www.xuz2.com"}
            }
          patient.analyseddata_attributes={
              "0" => {stime: Time.now-60*60*2, etime: Time.now, url: "www.result.com"}
            }
          patient.save
          result="added"
        end

      end
      # for datacore's instance methods
      def self.extended query
        def query.to_h
         
        end

        def query.to_dbm
         
        end
      end
    end
  end
end
include Query::ExtModel
